package ca.qc.bdeb.info.controleur;

import ca.qc.bdeb.info.application.Facade;
import java.sql.ResultSet;
import javax.servlet.http.HttpServletRequest;

public class CommandeConnexion implements Commande {

    private String next;

    public CommandeConnexion(String next) {
        this.next = next.toLowerCase();
    }

    @Override
    public String executer(HttpServletRequest req) throws CommandeException {
//        String user = req.getParameter("username");
//        String pwd = req.getParameter("password");
//        String query = "select AES_DECRYPT(password,'" + ConstantesWeb.CLE_CRYPTAGE
//                + "') pwd from user where username = '" + user + "' and password = AES_ENCRYPT('"
//                + pwd + "','" + ConstantesWeb.CLE_CRYPTAGE + "')";
//        ConnexionBD con;
//
//        try {
//            con = new ConnexionBD();
//            ResultSet set = con.queryExec(query);
//

//        } catch (Exception ex) {
//        }
        Facade f = new Facade();
        ResultSet set = f.getUsager(req.getParameter("username"), req.getParameter("password"));

        try {
            if (set != null) {
                if (set.first()) {
                    req.setAttribute("id", set.getString("id"));
                    req.setAttribute("user", set.getString("username"));
                    req.setAttribute("pwd", set.getString("pwd"));
                } else {
                    return ConstantesWeb.PAGE_CONNEXION_INVALIDE;
                }
            }
        } catch (Exception e) {
        }
        return next;
    }
}
